package com.yqg.manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.ITemplateService;
import com.yqg.manage.entity.Template;

import org.springframework.web.bind.annotation.RestController;

/**
 * TemplateController 前端控制器
 * @author suzhuang
 * @since 2023-07-05
 */
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Resource
    private ITemplateService templateService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody Template template) {
        template.setUploadDate(TimeUtils.getFormattedTime(new Date()));
        return Result.success(templateService.saveOrUpdate(template));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Template template =templateService.getById(id);
        template.setIsDelete("1");
        template.setUploadDate(TimeUtils.getFormattedTime(new Date()));
        return Result.success(templateService.saveOrUpdate(template));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<Template> list=  templateService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(templateService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(templateService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

